package com.aoxon.cargo.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.PubPicturePagerActivity;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.cache.SupCache;
import com.aoxon.cargo.loader.SupInfoLoader;
import com.aoxon.cargo.view.MyGallery;

public class PurGoodsPagerAdapter extends PagerAdapter {

	private ViewPager viewPager;

	private List<Cloth> cloths;
//	private int clothCount;
	private LayoutInflater layoutInflater;
	private Activity activity;
	private int width;

	private SupInfoLoader supInfoLoader;

	public PurGoodsPagerAdapter(Activity activity, ViewPager viewPager,
			List<Cloth> cloths) {
		this.cloths = cloths;
//		this.cloths = Collections.synchronizedList(cloths);
//		clothCount = cloths.size();
		this.activity = activity;
		this.viewPager = viewPager;
		
		supInfoLoader = new SupInfoLoader(activity);
		this.layoutInflater = LayoutInflater.from(activity);

		DisplayMetrics metric = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		width = metric.widthPixels; // 屏幕宽度（像素）
	}

	
	public void clear(){
		supInfoLoader.clear(false);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cloths.size();
	}

	@Override
	public View instantiateItem(ViewGroup container, int position) {

		GalleryAdapter adapter;
		final Cloth cloth = cloths.get(position);

		ScrollView item = (ScrollView) layoutInflater.inflate(
				R.layout.pur_goods_details, null);
		TextView tvGoodsName = (TextView) item
				.findViewById(R.id.tvPurGoodsDetailsName);
		TextView tvGoodsNumber = (TextView) item
				.findViewById(R.id.tvPurDetailsGoodsNumber);
		TextView tvGoodsPrice = (TextView) item
				.findViewById(R.id.tvPurGoodsDetailsPrice);
		TextView tvSupCompany = (TextView) item
				.findViewById(R.id.tvPurGoodsDetailsCompany);
		TextView tvSupAddress = (TextView) item
				.findViewById(R.id.tvPurGoodsDetailsAddress);
		TextView tvGoodDescr = (TextView) item
				.findViewById(R.id.tvPurGoodsDetailsGoodDescription);
		ImageView ivSupCertification = (ImageView) item
				.findViewById(R.id.ivPurGoodsDetailsCertificate);
		MyGallery gallery = (MyGallery) item
				.findViewById(R.id.glPurGoodsDetailsGalery);
		RelativeLayout rlSupInformation = (RelativeLayout) item
				.findViewById(R.id.rlPurGoodsDetailSupInfo);

		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gallery
				.getLayoutParams();
		params.height = (int) (width);
		gallery.setLayoutParams(params);
		gallery.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(activity,
						PubPicturePagerActivity.class);
				PubPicturePagerActivity.SHOW_ITEM = position;
				SupCache.pictureList = cloth.getPictures();
				PubPicturePagerActivity.ROOT = false;
				activity.startActivity(intent);
			}
		});

		if (cloth != null) {
			try {
				supInfoLoader.displayInfo(cloth.getSupId(), tvSupCompany,
						tvSupAddress, ivSupCertification, rlSupInformation);
				tvGoodsName.setText(cloth.getClothName());
				tvGoodsNumber.setText("款号：" + cloth.getClothItemNumber());
				tvGoodsPrice.setText("￥ " + cloth.getPrices().get(0).getPrice()
						+ "/件");
				if(cloth.getClothDescribe() != null){
					tvGoodDescr.setText(cloth.getClothDescribe());
				}
				
				if (cloth.getPictures() != null) {
					adapter = new GalleryAdapter(activity.getBaseContext(),
							cloth.getPictures(), true);
					gallery.setAdapter(adapter);
					gallery.setPager(viewPager);
					adapter.notifyDataSetChanged();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			// checkCollected(cloth);
		}

		container.addView(item, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);

		return item;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

}
